<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Session;

class Task extends Frontend {

    public function _initialize() {
        parent::_initialize();
        $this->assign('menu', 2);
    }

    public function index() {
        $s = input('s', 1);
        $this->assign('s', $s);
        return $this->view->fetch();
    }

    public function item() {
        $s = input('s', 1);  //1-待上刊  10-进行中  20-已完成  90-已失效
        $p = input('p', 0);
        $size = input('psize', 10);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }


        $task_list = \think\Db::table('fa_order')
                ->alias('o')
                ->field('t.*,o.order_id,o.order_no,o.order_status,o.car_id,o.createtime as ordercreatetime,o.updatetime as orderupdatetime')
                ->join('fa_task t', 'o.task_id = t.task_id');

        $task_list->where('o.user_id', $this->iUserId);
        if ($s == 1) {
            $task_list->where('order_status', 0);
//            $where['order_status'] = 0;
        }

        if ($s == 10) {
            $task_list->where('order_status', 'in', [1, 2, 10, 11, 12,13]);
        }

        if ($s == 20) {
            $task_list->where('order_status', 20);
        }

        if ($s == 90) {
            $task_list->where('order_status', 'in', [90, 91, 92]);
        }
//                ->where($where)
        $task_list = $task_list->order('o.createtime', 'desc')
                ->limit($size)->page($page)
                ->select();


//        echo \think\Db::getLastSql();die;
        foreach ($task_list as &$val) {
            $val['xg_img'] = explode(',', $val['xg_img']);
//            $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
            $val['ordercreatetime'] = date('Y-m-d H:i:s', $val['ordercreatetime']);
            $val['orderupdatetime'] = date('Y-m-d H:i:s', $val['orderupdatetime']);

            if ($s == 10) {
                $tempo_num = \think\Db::table('fa_order_tempo')
                        ->where('order_id', $val['order_id'])
                        ->order('tempo_num', 'desc')
                        ->value('tempo_num');

                if ($tempo_num) {
                    $val['tempo_num'] = $tempo_num;
                } else {
                    $val['tempo_num'] = 1;
                }
            }
        }
        unset($val);
//        dump($task_list);die;

        $this->assign('list', $task_list);
        return $this->view->fetch();
    }

    public function detail() {
        $id = input('id', 0);

        if ($id == 0) {
            $this->error('没有找到订单');
        }

        $task_info = \think\Db::table('fa_order')
                ->alias('o')
                ->field('t.*,o.order_id,o.order_no,o.order_status,o.car_id,o.createtime as ordercreatetime,o.updatetime as orderupdatetime,o.position_id')
                ->join('fa_task t', 'o.task_id = t.task_id');

        $task_info->where('o.user_id', $this->iUserId);
        $task_info->where('o.order_id', $id);

        $task_info = $task_info->find();

        if (!empty($task_info)) {
            $task_info['xg_img'] = explode(',', $task_info['xg_img']);

            $task_info['position_info'] = \think\Db::name('position')->where(array('position_id' => $task_info['position_id']))->find();

            if ($task_info['order_status'] == 2 || $task_info['order_status'] == 12) {
                $refusal_reason = \think\Db::name('order_tempo')->where(array('order_id' => $id,'status'=>20))->order('refusaltime desc')->value('refusal_reason');
                
                $this->assign('refusal_reason',$refusal_reason);
            }
        }

        $push_position = \think\Db::table('fa_order')
                ->alias('o')
                ->join('fa_task t', 'o.task_id = t.task_id')
                ->where(['o.order_id' => $id, 'o.user_id' => $this->iUserId])
                ->value('t.push_position');

        if ($push_position) {
            $push_position = explode(',', $push_position);

            $position_list = \think\Db::name('car_position')->where('fid', 'in', $push_position)->select();
            $this->assign('position_list', $position_list);
        }

        $this->assign('task_info', $task_info);

        return $this->view->fetch();
    }

    public function cancelTask() {
        $id = input('id', 0);

        if ($id == 0) {
            $this->error('没有找到订单');
        }

        $data['order_status'] = 90;

        $res = \think\Db::name('order')->where(array('order_id' => $id, 'user_id' => $this->iUserId))->update($data);

        if ($res) {
            $this->success('取消成功');
        } else {
            $this->error('取消失败');
        }
    }

    public function sk() {
        $oid = input('oid', 0);

        $push_position = \think\Db::table('fa_order')
                ->alias('o')
                ->join('fa_task t', 'o.task_id = t.task_id')
                ->where(['o.order_id' => $oid, 'o.user_id' => $this->iUserId])
                ->value('t.push_position');

        if ($push_position) {
            $push_position = explode(',', $push_position);

            $position_list = \think\Db::name('car_position')->where('fid', 'in', $push_position)->select();
        }

        if ($_POST) {
            $img_list = input('img_list');
            $img_list = \GuzzleHttp\json_decode($img_list);

            $flag = 0;
            $img_data = [];
            foreach ($img_list as $val) {
                $val[0] = substr($val[0], 13);

                $img_data[$val[0]] = $val[1];
                foreach ($position_list as $row) {
                    if ($val[0] == $row['car_position_id']) {
                        $flag++;
                        continue;
                    }
                }
            }

            if ($flag < count($position_list)) {
                $this->error('上刊信息不完整');
            }

            $task_info = \think\Db::table('fa_order')
                    ->alias('o')
                    ->field('t.*,o.order_id,o.order_no,o.order_status,o.car_id,o.createtime as ordercreatetime,o.updatetime as orderupdatetime,o.position_id')
                    ->join('fa_task t', 'o.task_id = t.task_id')
                    ->where('o.order_id', $oid)
                    ->find();


            $sk_start_time = $task_info['sk_start_time'];
            $sk_end_time = $task_info['sk_end_time'];

            if (time() < $sk_start_time) {
                $this->error('还未到上刊时间');
            }
            if (time() > $sk_end_time) {
                $this->error('已超过最后上刊时间');
            }

            $diff_time = timeDiff($sk_start_time, time());

            //计算当前时间是第几周
            $now_week = ceil($diff_time['day'] / 7);

            if ($now_week > $task_info['show_time']) {
                $this->error('已超过最后上刊时间');
            }

            $tempo_info = \think\Db::table('fa_order_tempo')
                    ->where('order_id', $oid)
                    ->where('tempo_num', $now_week)
                    ->where('status', 'in', '0,10')
                    ->find();

            if ($tempo_info) {
                $this->error('请不要重复提交上刊');
            }

            ksort($img_data, 1);
            $data['order_id'] = $oid;
            $data['tempo_num'] = $now_week;
            $data['img_list'] = implode(',', $img_data);
            $data['createtime'] = time();
            $res = \think\Db::name('order_tempo')->insert($data);

            if ($res) {
                $order_data['order_status'] = 11;

                $res = \think\Db::name('order')->where(array('order_id' => $oid, 'user_id' => $this->iUserId))->update($order_data);

                $this->success('上刊成功', url('index/task/skList', 'oid=' . $oid));
            } else {
                $this->error('上刊失败');
            }
        } else {

            if ($oid != 0) {

                if ($position_list) {
                    $this->assign('position_list', $position_list);
                }

                $this->assign('oid', $oid);
            }

            $this->assign('title', '完成上刊');
            return $this->view->fetch();
        }
    }

    public function skList() {
        $oid = input('oid', 0);

        $tempo_list = \think\Db::table('fa_order_tempo')
                ->alias('ot')
                ->field('ot.*')
                ->join('fa_order o', 'o.order_id = ot.order_id')
                ->where('o.user_id', $this->iUserId)
                ->where('o.order_id', $oid)
                ->order('createtime', 'desc')
                ->select();


        $push_position = \think\Db::table('fa_order')
                ->alias('o')
                ->join('fa_task t', 'o.task_id = t.task_id')
                ->where(['o.order_id' => $oid, 'o.user_id' => $this->iUserId])
                ->value('t.push_position');

        if ($push_position) {
            $push_position = explode(',', $push_position);

            $position_list = \think\Db::name('car_position')->where('fid', 'in', $push_position)->order('car_position_id', 'asc')->select();
            $this->assign('position_list', $position_list);
        }

        foreach ($tempo_list as &$val) {
            $val['img_list'] = explode(',', $val['img_list']);
        }
        unset($val);

        $this->assign('oid', $oid);
        $this->assign('list', $tempo_list);
        $this->assign('title', '任务进度');
        return $this->view->fetch();
    }

}
